package com.xy.exception;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import com.xy.constant.ErrorCode;

public class P2PHandlerException implements HandlerExceptionResolver {
	private Logger log = Logger.getLogger(P2PHandlerException.class);

	public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object arg2, Exception exception) {
		// 参数 exception，就是当前捕获的框架中的异常

		// 另一类：非自定义异常,统一定义错误码给result.jsp
		String resultCode = ErrorCode.ERROR;
		if (exception instanceof P2PException) {
			// 一类，自定义异常。给异常中的错误码给result.jsp
			resultCode = ((P2PException) exception).getErrorCode();
		}

		// TODO !!!!!!!! 异常日志！！！！！！！！！！！！！
		log.info("异常日志：", exception);

		ModelAndView mv = new ModelAndView();
		mv.addObject("resultCode", resultCode);
		mv.setViewName("/pages/result.jsp");
		return mv;
	}

}
